package com.ylsk.common.utils;

import com.google.common.collect.Maps;

import java.lang.reflect.Field;
import java.util.Map;

public final class ClassUtils {


    private static ThreadLocal<Map<String, Field>> threadLocal = new ThreadLocal();


    private ClassUtils() {

    }

    public static Map<String/*字段名*/, Field/*字段Filed*/> listAllFields(Class clazz) {
        Map<String, Field> retMap = threadLocal.get();
        if (retMap != null) {
            return retMap;
        }
        retMap = Maps.newHashMap();
        while (Object.class != clazz) {
            Field[] declaredFields = clazz.getDeclaredFields();
            for (Field declaredField : declaredFields) {
                retMap.put(declaredField.getName(), declaredField);
            }
            clazz = clazz.getSuperclass();
        }
        threadLocal.set(retMap);
        return retMap;
    }

    public static void remove() {
        threadLocal.remove();
    }
}
